package com.mogu.biguser.common.constant;



import com.mogu.biguser.common.util.DateUtil;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 常用工具类，字符串、数字相关
 *
 * @author XiongNeng
 * @version 1.0
 * @since 2018/1/15
 */
public class CommonUtil {
    /**
     * 检查某版本是否比现在版本更大些
     *
     * @return 是否版本数更大
     */

    public static void main(String[] args) {
    /*    System.err.println( 0.1+-02); //测试
        String time1="0:00";
        String time2="2:00";
        String time3="1:00";
       Date date1= DateUtil.parse(time1,"HH:mm");
        Date date2= DateUtil.parse(time2,"HH:mm");
        Date date3= DateUtil.parse(time3,"HH:mm");
        if (date1.getTime()<date3.getTime()&&date2.getTime()>date3.getTime()){
            System.out.println("1111");*/
       // String url="http://115.231.254.38:9090/mogo/swxt/1539846463306.doc";

        /*Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parse("2018-12-31","yyyy-MM"));
        calendar.add(calendar.MONTH,1);
        String s = DateUtil.NextMonth("2018-12-31");

        System.out.println(s);
        System.out.println(calendar.getTime());*/
        String avgMonth ="1000";
        String[] split = avgMonth.split("-");



    }
    //小数转化为百分比
    public static String changeForPercent(double num){
        NumberFormat nf = NumberFormat.getPercentInstance();
        nf.setMaximumFractionDigits(2);
        return nf.format(num);
    }

    //获取前几月的年月日的时间
    public static String getFewMTime(int num){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -num);
        Date m = c.getTime();
        String mon = format.format(m);
        return mon;
    }
    //获取日区间
    public static List<String> getDatesBetweenTwoDate(String beginDate, String endDate) throws ParseException {
        List<String> lDate = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse(beginDate);//定义起始日期

        Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);//定义结束日期

        lDate.add(beginDate);// 把开始时间加入集合
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(d1);
        boolean bContinue = true;
        while (bContinue) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后
            if (d2.after(cal.getTime())) {

                lDate.add(sdf.format(cal.getTime()));
            } else {
                break;
            }
        }
        lDate.add(endDate);// 把结束时间加入集合
        return lDate;
    }

        //获取区间内的几月的年月日的时间
    public static List<String>  getMonByS_E(String startTime,String endTime){

        List<String> list=new ArrayList<>();
        try {
            Date d1 = new SimpleDateFormat("yyyy-MM").parse(startTime);//定义起始日期

            Date d2 = new SimpleDateFormat("yyyy-MM").parse(endTime);//定义结束日期

            Calendar dd = Calendar.getInstance();//定义日期实例

            dd.setTime(d1);//设置日期起始时间

            while (dd.getTime().before(d2)) {//判断是否到结束日期

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

                String str = sdf.format(dd.getTime());

             /*   System.out.println(str);//输出日期结果*/
                list.add(str);

                dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
            }
            list.add(endTime.substring(0,7));
        }catch ( Exception e){
            e.printStackTrace();
        }
        return  list;
    }


    //获取前几日的年月日的时间
    public static String getFewTime(int num){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -num);
        Date m = c.getTime();
        String mon = format.format(m);
        return mon;
    }

    //获取今日的年月日时间
    public static String getTodayTime(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        Date m = cal.getTime();
        String mon = format.format(m);

        return mon;
    }

    //获取昨日的年月日时间
    public static String getYesterdayTime(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -1);
        Date m = cal.getTime();
        String mon = format.format(m);
        return mon;
    }
    //保留两位有效小数
    public static double keepTwoData(Double num){
        if(num!=null){
            BigDecimal b = new BigDecimal(num);
            return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        return 0.0;
    }

    //保留四位有效小数
    public static double keepFourData(Double num){
        if(num!=null){
            BigDecimal b = new BigDecimal(num);
            return b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        return 0.0;
    }
    public static boolean isNewer(String version, String nowVersion) {
        try {
            String[] versions = version.split("\\.");
            String[] nowVersions = nowVersion.split("\\.");

            if (versions.length != nowVersions.length) {
                return false;
            }
            int sum = 0;
            for (String v : versions) {
                sum += sum * 10 + Integer.valueOf(v);
            }

            int nowSum = 0;
            for (String nv : nowVersions) {
                nowSum += nowSum * 10 + Integer.valueOf(nv);
            }

            return sum > nowSum;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}
